<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter();
//控制注册与登录表单的显示，默认显示登录
//导入token状态
import { useTokenStore } from '@/stores/token.js'

//调用useTokenStore得到状态
const tokenStore = useTokenStore();


//定义数据模型
const registerData = ref({
    userID: '',
    password: '',
    code: '',
    uid: '',
    type: ''
    // repwd:''
})
// //校验密码函数
// const checkRepwd=(rule,value,callback)=>{
//     if(value=='')
//     {
//         callback(new Error('请再次确认密码'))
//     }else if(value!=registerData.value.pwd)
//     {
//         callback(new Error('请确保两次输入的密码一样'))
//     }else{
//         callback()
//     }
// }
//定义表单校验规则
const rules = {
    userID: [
        { required: true, message: '请输入学号', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ],
    uid: [
        { required: true, message: '请输入验证码', trigger: 'blur' }
    ]
    // ,repwd:[
    //     {check}
    // ]
}
//导入pinia
import { useAdmInfoStore } from '@/stores/adm.js'
//调用后台接口，完成注册
import { admLoginService, getVerifiCodeService, getInfo } from '@/api/adm/adm.js'
//导入pinia
const admInfoStore = useAdmInfoStore()
const check = ref(false)
const login = async () => {
    registerData.value.type = check.value
    let result = await admLoginService(registerData.value);
    // if(result.code==0){
    //     alert('登录成功！')
    // }else alert('登录失败！')
    // alert('登录成功！');
    //保存token
    tokenStore.setToken(result.data)
    ElMessage.success('登录成功！');
    if (check.value) {//记住我
        localStorage.setItem('UserloginInfo', JSON.stringify(registerData.value))
    } else {
        localStorage.setItem('UserloginInfo', JSON.stringify({}))
    }
    result = await getInfo()
    admInfoStore.info = result.data;
    router.push('/usermanage')
}
const url = ref()
const getVerifiCode = () => {
    registerData.value.uuid = new Date().getTime()
    url.value = "http://localhost:80/login/getVerify/" + registerData.value.uuid
}
//页面加载时监听是否记录
onMounted(
    () => {
        getVerifiCode()
        if (localStorage.getItem('UserloginInfo') != null) {
            if (localStorage.getItem('UserloginInfo').length >= 2) {
                check.value = true;
                const loginInfo = JSON.parse(localStorage.getItem('loginInfo'))
                registerData.value.userID = loginInfo?.userID
                registerData.value.password = loginInfo == null ? '' : loginInfo.password
            }
        } else {
            check.value = false
        }
    }
)
// result.msg?result.msg:
</script>
<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <!-- 登录表单 -->
            <el-form ref="form" size="large" autocomplete="off" :model="registerData" :rules="rules">
                <el-form-item>
                    <h1>登录</h1>
                </el-form-item>
                <el-form-item prop="userID">
                    <el-input :prefix-icon="User" placeholder="请输入学工号" v-model="registerData.userID"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="registerData.password" show-password></el-input>
                </el-form-item>
                <!-- 验证码输入框 -->
                <el-form-item prop="code">
                    <el-input v-model="registerData.code" type="text" placeholder="请输入验证码" />
                </el-form-item>
                <!-- 验证码图片 -->
                <el-form-item>
                    <el-image style="width: 130px; height: 33px" @click="getVerifiCode" :src="url" />
                </el-form-item>
                <el-form-item class="flex">
                    <div class="flex">
                        <el-checkbox v-model="check">记住我</el-checkbox>
                        <el-link type="primary" href="/adm/login">管理员登录-></el-link>
                    </div>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
                </el-form-item>
                <!-- <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
                        注册 →
                    </el-link>
                </el-form-item> -->
            </el-form>
        </el-col>
    </el-row>
</template>
<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background:
            url('@/assets/bg1.png') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>